package org.projects.graduates.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.projects.graduates.page.Pager;

import com.dayatang.domain.AbstractEntity;
import com.dayatang.domain.QuerySettings;

@Entity
@Table(name = "post_reply")
public class PostReply extends AbstractEntity {

	private static final long serialVersionUID = 3846709116904052588L;

	private final static String QL = "select count(o.id) from org.projects.graduates.domain.PostReply o ";
	@OneToOne
	private TimeTable timeTable;

	@OneToOne
	private Post post;

	@OneToMany
	private List<Reply> replies;
	
	public PostReply() {
		super();
	}

	public PostReply(TimeTable timeTable, Post post) {
		super();
		this.timeTable = timeTable;
		this.post = post;
	}

	public static List<PostReply> page(TimeTable timeTable, long currentPage, long pageSize) {
		List<PostReply> results = new ArrayList<PostReply>();
		QuerySettings<PostReply> settings = QuerySettings.create(PostReply.class).eq("timeTable", timeTable);
		Pager pager = getPager(currentPage, pageSize);
		settings.setFirstResult(Integer.parseInt("" + pager.startLine()));
		settings.setMaxResults(Integer.parseInt("" + pageSize));
		results.addAll(getRepository().find(settings));
		return results;
	}

	public static Pager getPager(long currentPage, long pageSize) {
		return new Pager(count(), pageSize, currentPage);
	}

	public static long count() {
		return count(null);
	}

	@SuppressWarnings("unchecked")
	public static long count(String query) {
		String ql = QL ;
		if(StringUtils.isNotEmpty(query)){
			ql += query;
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		@SuppressWarnings("rawtypes")
		List<Long> count = new ArrayList(getRepository().find(ql, params));
		return null == count ? 0 : count.get(0).intValue();
	}
	
	public static PostReply findBy(Post post) {
		QuerySettings<PostReply> qs = QuerySettings.create(PostReply.class);
		qs.eq("post", post);
		return getRepository().getSingleResult(qs);
	}

	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(this.getId()).toHashCode();
	}

	@Override
	public boolean equals(Object other) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}

	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
	}

	public List<Reply> getReplies() {
		return replies;
	}

	public void setReplies(List<Reply> replies) {
		this.replies = replies;
	}

	public TimeTable getTimeTable() {
		return timeTable;
	}

	public void setTimeTable(TimeTable timeTable) {
		this.timeTable = timeTable;
	}



}
